<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>动态轨迹</title>
  <script include="jquery" src="./static/libs/include-lib-local.js"></script>
  <script include="language,proj4,mapv" src="./static/libs/include-mapboxgl-local.js"></script>
  <link href="./static/demo/mapboxgl/example/style.css" rel="stylesheet" type="text/css" />
  <style>
    body {
      margin: 0;
      padding: 0;
    }

    #map {
      position: absolute;
      top: 0;
      bottom: 0;
      width: 100%;
    }
  </style>
</head>

<body>
  <div id="map">
    <div id="mouse-position">
    </div>
  </div>
  <script>
    //一定要去mapbox注册一个key,这个key会失效的
    mapboxgl.accessToken = 'pk.eyJ1IjoicGFybmRlZWRsaXQiLCJhIjoiY2o1MjBtYTRuMDhpaTMzbXhpdjd3YzhjdCJ9.sCoubaHF9-nhGTA-sgz0sA';
    var map = new mapboxgl.Map({
      container: 'map', // 绑定div
      style: 'mapbox://styles/mapbox/dark-v9', // 使用MapBox官方的样式
      center: [114.321317, 30.398428],
      zoom: 10
    });

    map.addControl(new MapboxLanguage(), 'top-right');//中文支持
    map.addControl(new mapboxgl.NavigationControl(), 'top-left');
    //注册鼠标移动事件
    map.on('mousemove', function (e) {
      //经纬度坐标转web墨卡托
      const earthRad = 6378137.0;
      const x = e.lngLat.lng * Math.PI / 180 * earthRad;
      const a = e.lngLat.lat * Math.PI / 180;
      const y = earthRad / 2 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
      document.getElementById('mouse-position').innerHTML = "X轴：" + x.toFixed(2) + "，Y轴：" + y.toFixed(2);
    });
    $.get('../../static/data/mapv/wuhan-car.txt', function (rs) {
      var data = [];
      var timeData = [];
      rs = rs.split("\n");
      var maxLength = 0;
      for (var i = 0; i < rs.length; i++) {
        var item = rs[i].split(',');
        var coordinates = [];
        if (item.length > maxLength) {
          maxLength = item.length;
        }
        for (j = 0; j < item.length; j += 2) {
          if (item.length === 1) {
            continue;
          }
          coordinates.push(proj4('EPSG:3857', 'EPSG:4326', [item[j], item[j + 1]]));
          timeData.push({
            geometry: {
              type: 'Point',
              coordinates: proj4('EPSG:3857', 'EPSG:4326', [item[j], item[j + 1]])
            },
            count: 1,
            time: j
          });
        }
        data.push({
          geometry: {
            type: 'LineString',
            coordinates: coordinates
          }
        });

      }

      var dataSet = new mapv.DataSet(data);

      var options = {
        context: '2d',
        strokeStyle: 'rgba(53,57,255,0.5)',
        coordType: 'bd09mc',
        // globalCompositeOperation: 'lighter',
        shadowColor: 'rgba(53,57,255,0.2)',
        shadowBlur: 3,
        lineWidth: 3.0,
        draw: 'simple'
      };

      new mapboxgl.zondy.MapvLayer(map, dataSet, options);


      var dataSet = new mapv.DataSet(timeData);

      var options = {
        context: '2d',
        fillStyle: 'rgba(255, 250, 250, 0.2)',
        coordType: 'bd09mc',
        globalCompositeOperation: "lighter",
        size: 1.5,
        animation: {
          stepsRange: {
            start: 0,
            end: 100
          },
          trails: 3,
          duration: 5,
        },
        draw: 'simple'
      };

      new mapboxgl.zondy.MapvLayer(map, dataSet, options);
    });
  </script>


</body>

</html>